# -*- coding: utf-8 -*-
import scrapy

from dirbot.items import UmeiItem


class UmeiSpider(scrapy.Spider):
    name = "umei"
    allowed_domains = ["umei.cc"]
    start_urls = (
        'http://www.umei.cc/p/gaoqing/gangtai/20160620193150.htm',
    )

    def parse_imgs(self, response):
        print("Image Spider")
        for sel in response.xpath('//img'):
            print(sel.extract())
            item = UmeiItem()
            item['title'] = sel.xpath('@title').extract()
            item['src'] = sel.xpath('@src').extract()
            item['image_urls'] = item['src']
            yield item

    def parse(self, response):
        self.parse_imgs(response)
        print("Next Page! >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
        for next in response.xpath('//div[@class="pages"][2]'):
            print(next.extract())
            print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
            for url in next.xpath('./a[@href]').xpath('//@href').extract():
                print("<><><><><><><><><><><><><><><><><><><>")
                print(url)
                yield scrapy.Request('http://www.umei.cc/p/gaoqing/gangtai/' + url, callback=self.parse_imgs)


